package org.erepublik.company.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Klasa z metodami do odczytu stron internetowych
 * 
 * @author Kacper Gorski 'Railman' railman85@gmail.com
 */
public class WebpageReader {

    /**
     * @param urlAsString
     * @return zamienia podany adres url na strumien
     * @throws IOException
     */
    public static InputStream getStreamToIrl(String urlAsString) throws IOException {
        URL url = new URL(urlAsString);
        return url.openStream();
    }

    /**
     * @param urlAsString
     * @return zamienia podany adres url na strumien
     * @throws IOException
     */
    public static String getPage(String urlAsString) throws IOException {
        URL url = new URL(urlAsString);
        return Utils.convertStreamToString(url.openStream());
    }

    public static JSONObject getJSONFromErepublikApi(String url) throws MalformedURLException, IOException, JSONException {

        String jsonAsString = Utils.convertStreamToString(new URL(url).openStream());

        return new JSONObject(jsonAsString);
    }

}
